﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace PivotBeer
{
    class BeerSpider
    {
        public BeerSpider(){

        }

        public List<string> ParseStylesPage()
        {
            string html = GetHtml("http://www.beeradvocate.com/beer/style");
            
        
            string[] foo = {"/beer/style/"};
            List<string> urls = new List<string>();
            string basestring = "http://www.beeradvocate.com/beer/style/";
            string[] styles = html.Split(foo, StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in styles)
            {
                if (!s.Contains("\""))
                {
                    continue;
                }
                try
                {
                    urls.Add(basestring + Int32.Parse(s.Substring(0, s.IndexOf('"'))).ToString());
                }
                catch (FormatException)
                {
                    continue;
                }
            }
            return urls;  
          
          
        }

        public List<string> GetBeerPages(string url)
        {


            string html = GetHtml(url);

            string[] foo = { "/beer/profile/" };
            List<string> urls = new List<string>();
            string basestring = "http://www.beeradvocate.com/beer/profile/";
            string[] beers = html.Split(foo, StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in beers)
            {
                string actual = s.Substring(0,s.IndexOf('"'));
                if (!actual.Contains("/"))
                {
                    continue;
                }
                string test = actual[actual.Length - 1].ToString();
                
                try
                {
                    Int32.Parse(test);
                }
                catch (FormatException)
                {
                    continue;
                }
                urls.Add(basestring + actual);
            }
            return urls;  
        }

        public static string GetHtml(string url)
        {
            StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[40000];
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            // execute the request
            
            HttpWebResponse response = (HttpWebResponse)
                request.GetResponse();
         


            // we will read data via the response stream
            Stream resStream = response.GetResponseStream();

            string tempString = null;
            int count = 0;

            do
            {
                // fill the buffer with data
                count = resStream.Read(buf, 0, buf.Length);

                // make sure we read some data
                if (count != 0)
                {
                    // translate from bytes to ASCII text
                    tempString = Encoding.Default.GetString(buf, 0, count);

                    // continue building the string
                    sb.Append(tempString);
                }
            }
            while (count > 0); // any more data to read?
            return sb.ToString();
        }

    }
}
